﻿<%@ Page Language="C#" %>

<script runat="server">

    protected override void OnLoad(EventArgs e)
    {
        //PaymentFactory.Log(this.Context);

        IPaymentIPN ipn = PaymentFactory.GetPaymentIPN(this.Context);

        //PaymentFactory.Log(
        //    this.Context,
        //    "VerifyState：" + ipn.VerifyState,
        //    "PaymentState：" + ipn.PaymentState.ToString());

        if (ipn.VerifyState == PaymentVerifyState.Verified && ipn.PaymentState != PaymentState.Unknown)
        {
            int orderId = ipn.OrderId.As<int>();
            OrderStatus orderState = Orders.PaymentStateToOrderState(ipn.PaymentState);

            //PaymentFactory.Log(
            //    this.Context,
            //    "OrderId：" + orderId,
            //    "OrderState：" + orderState.ToString());

            Orders.UpdateStatus(
                orderId,
                orderState,
                true);

            PaymentInfo payment = new PaymentInfo();
            payment.BankName = ipn.PaymentMethod;
            payment.BankAccounts = ipn.PaymentAccount;
            payment.CurrencyType = ipn.PaymentCurrency;
            payment.DateCreated = DateTime.Now;
            payment.Dates = DateTime.Now;
            payment.OrderId = orderId;
            payment.Remark = string.Empty;
            payment.Amount = ipn.PaymentAmount;

            Orders.UpdatePayment(payment);
        }
    }
    
</script>